/*
 * Description: HOST服务提供给GUEST函数类
 */

#include "sendto_host.h"
#include <string>
#include <fstream>
#include "log.h"
#include "common.h"
#include "error.h"
#include "guest2host.pb.h"
#include "host2guest.pb.h"

using namespace COMMON;

namespace WINCONTROL {

int SendToHost::ConnectServer()
{
    LOG_INFO("connect server");
    int port = 0;
    return EstablishEventChannel(port);
}

int SendToHost::DisConnectServer()
{
    LOG_INFO("disconnect server!");
    int ret = CloseEventChannel();
    if (ret != OK) {
        LOG_ERR("DisConnectServer failed!");
    }
    return ret;
}

void SendToHost::BootComplete()
{
    LOG_INFO("BootComplete");
    Guest::protobuf::link::Void parameters;
    (void)CallMethodAsync(SendToHostEvent::BOOT_COMPLETE, parameters);
}

void SendToHost::Shutdown()
{
    LOG_INFO("Shutdown");
    Guest::protobuf::link::Void parameters;
    (void)CallMethodAsync(SendToHostEvent::SHUTDOWN, parameters);
}

void SendToHost::QuickStartShutdown()
{
    LOG_INFO("QuickStartShutdown");
    Guest::protobuf::link::Void parameters;
    (void)CallMethodAsync(SendToHostEvent::QUICKSTART_SHUTDOWN, parameters);
}

void SendToHost::GuestSessionChange(int32_t state)
{
    LOG_INFO("SessionChange");
    Guest::protobuf::GuestAgent::Integer parameters;
    parameters.set_value(state);
    (void)CallMethodAsync(SendToHostEvent::GUEST_SESSION_CHANGE, parameters);
}

void SendToHost::SendPerfRequestToHost(std::string scene, std::string appName, bool isStartSign)
{
    LOG_INFO("PerfRequest starting.");
    Guest::protobuf::link::PerfMsg perfMsg;
    perfMsg.set_scene(scene);
    perfMsg.set_appname(appName);
    perfMsg.set_isstartsign(isStartSign);
    (void)CallMethodAsync(SendToHostEvent::PERF_REQUEST, perfMsg);
}

void SendToHost::EnableOrDisableScreenLock(bool flag)
{
    LOG_INFO("Enable or Disable Screen lock ret {}", flag);
    Guest::protobuf::GuestAgent::Bool result;
    result.set_value(flag);
    (void)CallMethodAsync(SendToHostEvent::ENABLE_DISABLE_SCREEN_LOCK, result);
}

void SendToHost::UnmountUnattend()
{
    LOG_INFO("unmount unattend");
    Guest::protobuf::link::Void parameters;
    (void)CallMethodAsync(SendToHostEvent::UMOUNT_UNATTEND, parameters);
}

void SendToHost::SyncUpdateProcStatus(bool state)
{
    LOG_INFO("Sync update process status.");
    Guest::protobuf::GuestAgent::Bool parameters;
    parameters.set_value(state);
    (void)CallMethodAsync(SendToHostEvent::SYNC_UPDATE_PROC_STATUS, parameters);
}

#ifdef _FUZZ
int SendToHost::StartFuzz()
{
    LOG_INFO("StartFuzz");
    Guest::protobuf::link::Void parameters;
    Guest::protobuf::GuestAgent::Bool result;
    auto ret = CallMethodAsync(SendToHostEvent::STARTFUZZ, parameters, true);
    if (ret != OK) {
        LOG_ERR("Call continue vm failed. ret={}", ret);
    }
    return ret;
}

void SendToHost::FuzzMsg(int id, const google::protobuf::MessageLite &parameters)
{
    LOG_INFO("FuzzMsg");
    int ret = CallMethodAsync(id, parameters, true);
    if (ret < 0) {
        LOG_ERR("Restart guest async call error, ret {}", ret);
    }
}
#endif

}  // namespace WINCONTROL